package com.jstyle.jclifexd.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.amap.api.maps.model.LatLng;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.adapter.SharePreviewViewPagerAdapter;
import com.jstyle.jclifexd.fragment.ShareFirstFragment;
import com.jstyle.jclifexd.fragment.SharePictureFragment;
import com.jstyle.jclifexd.utils.GalleryTransformer;
import com.jstyle.jclifexd.utils.ImageUtils;
import com.jstyle.jclifexd.utils.ScreenUtils;
import com.jstyle.jclifexd.utils.Utils;
import com.jstyle.jclifexd.utils.WeakDataHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SharePreviewActivity extends BaseActivity {

    @BindView(R.id.viewpager_share_preview)
    ViewPager viewpagerSharePreview;

    String path;
    String mPathDate;
    String fastPace;
    String lowPace;
    public static final String sharePicture_Path = "sharePicture_Path";
    public static final String share_FastPace = "share_FastPace";
    public static final String share_lowstPace= "share_lowstPace";
    int pageSelected;
    private SharePreviewViewPagerAdapter sharePreviewViewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_preview);
        ButterKnife.bind(this);
        path = getIntent().getStringExtra(sharePicture_Path);
        mPathDate = getIntent().getStringExtra(ExerciseHistoryActivity.pathRecordDate);
        fastPace = getIntent().getStringExtra(share_FastPace);
        lowPace = getIntent().getStringExtra(share_lowstPace);
        init();
    }
    int pageSize=3;
    private void init() {
        List<LatLng>latLngList= WeakDataHolder.getInstance().getData();
        if(latLngList==null||latLngList.size()==0)pageSize=2;
        pageSelected=3-pageSize;
        viewpagerSharePreview.setOffscreenPageLimit(pageSize);
        viewpagerSharePreview.setPageMargin(ScreenUtils.dip2px(this, 4));
        viewpagerSharePreview.setPageTransformer(true, new GalleryTransformer());
         sharePreviewViewPagerAdapter=new SharePreviewViewPagerAdapter(getSupportFragmentManager(), ImageUtils.shootPath+path, mPathDate);
        sharePreviewViewPagerAdapter.setSize(pageSize);
        sharePreviewViewPagerAdapter.setPace(fastPace,lowPace);
        viewpagerSharePreview.setAdapter(sharePreviewViewPagerAdapter);
        viewpagerSharePreview.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                pageSelected=3-pageSize+i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    @OnClick(R.id.iv_sharePreview_back)
    public void onViewClicked() {
        finish();
    }

    @OnClick({R.id.rb_share_more})
    public void onViewClicked(View view) {
        if(Utils.isFastClick()){return;}
        switch (view.getId()) {


            case R.id.rb_share_more:
                shareByPhone();
                break;
        }
    }
    private void shareByPhone(){
        if(pageSelected==0){
            ShareFirstFragment shareFirstFragment=(ShareFirstFragment)sharePreviewViewPagerAdapter.getCurrentFragment();;
            shareFirstFragment.shareFile();
        }else if(pageSelected==1){
            ScreenUtils.shareByPhone(this,path);
        }else{
            SharePictureFragment sharePictureFragment= (SharePictureFragment) sharePreviewViewPagerAdapter.getCurrentFragment();
            sharePictureFragment.shareFile();
        }

    }
}
